﻿using BUSINESS;
using DataAccessObject;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace GUI.MonHoc
{
    /// <summary>
    /// Interaction logic for GUIMonHoc.xaml
    /// </summary>
    public partial class GUIMonHoc : Window
    {
        List<KHOILOP> listKhoiLop;
        List<MONHOC> listmonhoc = new List<MONHOC>();
        public GUIMonHoc()
        {
            InitializeComponent();
            /////////////////////
            this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            InitializeControl();
        }

        private void InitializeControl()
        {
            listKhoiLop = KhoiLopBUS.getAllKhoiLop();
            txtInputListKhoiLop.ItemsSource = listKhoiLop;
            txtListKhoiLopMonHoc.ItemsSource = listKhoiLop;
            if (txtInputListKhoiLop.Items.Count > 0)
            {
                txtInputListKhoiLop.SelectedIndex = 0;
                txtListKhoiLopMonHoc.SelectedIndex = 0;
            }
            ///////////////////////////////////////////////////
            txtListMonHoc.ItemsSource = MonHocBUS.getAllMonHoc();
            if (txtListMonHoc.Items.Count > 0) txtListMonHoc.SelectedIndex = 0;
            /////////////////////////////////////////////////////
            tbMonHocByKhoiLop.ItemsSource = listmonhoc;    
        }

        private void txtListMonHoc_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            reset();
            var monhoc = txtListMonHoc.SelectedItem as MONHOC;
            if (monhoc == null) return;
            txtInputTenMonHoc.Text = monhoc.TenMonHoc;
            var listCurrentKhoiLop = KhoiLopBUS.getKhoiLopByMonHoc(monhoc);
            int index;
            txtListKhoiLopMonHoc.SelectedItems.Clear();
            foreach (var currkhoilop in listCurrentKhoiLop)
            {
                index = -1;
                foreach (var khoilop in listKhoiLop)
                {
                    index++;
                    if (currkhoilop.KhoiLopId == khoilop.KhoiLopId)
                    {
                        txtListKhoiLopMonHoc.SelectedItems.Add(khoilop);
                    }
                }
            }
            updateTableMonHoc();
        }

        private void updateTableMonHoc()
        {
            listmonhoc.Clear();
            listmonhoc.AddRange(KhoiLopBUS.getAllMonHocByKhoiLop(txtInputListKhoiLop.SelectedItem as KHOILOP));
            tbMonHocByKhoiLop.Items.Refresh();
        }

        private void btnCapNhat_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                List<KHOILOP> listKhoiLop = txtListKhoiLopMonHoc.SelectedItems.OfType<KHOILOP>().ToList();
                var monhoc = txtListMonHoc.SelectedItem as MONHOC;
                MonHocBUS.updateChiTietMonHocByMonHoc(monhoc, listKhoiLop);
                updateSuccessed();
                updateTableMonHoc();
            }
            catch
            {
                updateFail();
            }
        }

        private void updateSuccessed()
        {
            txtError.Text = "*Cập nhật thành công!";
            txtError.Foreground = Brushes.Blue;
        }

        private void reset()
        {
            txtError.Text = "*Thao tác chưa được lưu!";
            txtError.Foreground = Brushes.Green;
        }

        private void updateFail()
        {
            txtError.Text = "*Cập nhật thất bại!";
            txtError.Foreground = Brushes.Red;
        }

        private void txtListKhoiLopMonHoc_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            reset();
        }

        private void txtInputListKhoiLop_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            updateTableMonHoc();
        }
    }
}
